package com.autumn.platform.core.context.response.impl;

import java.util.Locale;

import org.springframework.core.AttributeAccessorSupport;

import com.autumn.platform.core.context.response.IResponseContext;
import com.autumn.platform.core.exception.CodableException;
import com.autumn.platform.core.spring.SpringHolder;

public class ResponseContext extends AttributeAccessorSupport implements IResponseContext {

    /**
     * 
     */
    private static final long serialVersionUID = 555809890312923124L;

    private final Response response;

    public ResponseContext() {
        this.response = new Response();
    }

    public ResponseContext(String code, String message) {
        this.response = new Response(code, message);
    }

    @Override
    public String getCode() {
        return response.getCode();
    }

    @Override
    public void setCode(String code) {
        response.setCode(code);
    }

    @Override
    public void setExceptionCode(String code, Object... args) {
        this.setExceptionCode(code, null, args);
    }

    @Override
    public void setExceptionCode(String code, Locale locale, Object... args) {
        response.setExceptionCode(code, locale, args);
    }

    @Override
    public String getMessage() {
        return response.getMessage();
    }

    @Override
    public void setMessage(String message) {
        response.setMessage(message);
    }

    @Override
    public boolean isSuccess() {
        return response.isSuccess();
    }

    @Override
    public void setSuccess(boolean success) {
        response.setSuccess(success);
    }

    @Override
    public String getTrackId() {
        return response.getTrackId();
    }

    @Override
    public void setTrackId(String trackId) {
        response.setTrackId(trackId);
    }

    @Override
    public <E> E getAttribute(String name, Class<E> cls) {
        Object value = super.getAttribute(name);
        if (null != value) {
            return SpringHolder.convert(value, cls);
        }
        return null;
    }

    @Override
    public CodableException toException() {
        return response.toException();
    }
}
